package com.seqb.manage.user.controller;

import com.github.pagehelper.Page;
import com.seqb.annotation.Log;
import com.seqb.common.Query;
import com.seqb.common.ResultVo;
import com.seqb.common.controller.BaseController;
import com.seqb.manage.user.domain.UserRoleDO;
import com.seqb.manage.user.service.UserRoleService;
import com.seqb.utils.PageUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author lilinshen
 * @title 用户与角色对应关系
 * @description 请填写相关描述
 * @date 2019/11/15
 */
@Controller
@RequestMapping("/manage/user/userRole")
public class UserRoleController extends BaseController {
    @Autowired
    private UserRoleService userRoleService;

    @GetMapping()
    @RequiresPermissions("manage:user:userRole:list")
    @Log("用户与角色对应关系列表")
    String userRole() {
        return "manage/user/userRole.list";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("manage:user:userRole:list")
    @Log("用户与角色对应关系列表")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        Page<UserRoleDO> page = userRoleService.list(query);
        PageUtils pageUtils = new PageUtils(page.getResult(), page.getTotal());
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("manage:user:userRole:add")
    @Log("用户与角色对应关系添加")
    String add() {
        return "manage/user/userRole.add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("manage:user:userRole:edit")
    @Log("用户与角色对应关系编辑")
    String edit(@PathVariable("id") Long id, Model model) {
        UserRoleDO userRole = userRoleService.get(id);
        model.addAttribute("userRole", userRole);
        return "manage/user/userRole.edit";
    }

    /**
     * 添加保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("manage:user:userRole:add")
    @Log("用户与角色对应关系添加保存")
    public ResultVo save(UserRoleDO userRole) {
        if (userRoleService.save(userRole)) {
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 编辑保存
     */
    @ResponseBody
    @RequestMapping("/update")
    @Log("用户与角色对应关系编辑保存")
    @RequiresPermissions("manage:user:userRole:edit")
    public ResultVo update(UserRoleDO userRole) {
        userRoleService.update(userRole);
        return ResultVo.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("manage:user:userRole:remove")
    @Log("用户与角色对应关系删除")
    public ResultVo remove(Long id) {
        if (userRoleService.remove(id) > 0) {
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 批量删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("manage:user:userRole:batchRemove")
    @Log("用户与角色对应关系批量删除")
    public ResultVo remove(@RequestParam("ids[]") Long[] ids) {
        userRoleService.batchRemove(ids);
        return ResultVo.ok();
    }

}
